package com.ifp.weixin.biz.core.impl;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import org.xml.sax.InputSource;

import com.ifp.weixin.biz.core.BusLineSearch;
import com.ifp.weixin.constant.Constant;
import com.ifp.weixin.MessageMng.Message.bus.BusLine;
import com.ifp.weixin.util.HttpClientUtil;

import java.net.URLEncoder; 

import javax.faces.validator.LengthValidator;


@Service("busLineSearchService")
public class BusLineSearchImpl implements BusLineSearch {
	

	public List<BusLine> SearchBusLine(String city,String q,int with_xys) {
		String url=null;
		StringBuffer bfUrl =new StringBuffer();
		List<BusLine> list = new ArrayList<BusLine>();
	
		BusLine busLine =new BusLine();
		String searchStr = null;
		String searchResp = null;
		bfUrl.append(Constant.BUS_SEARCH_API_WEB+"?");
		bfUrl.append("app_key="+Constant.BUS_SEARCH_API_APPKEY+"&");
		try {
			bfUrl.append("city="+URLEncoder.encode(city,"UTF-8")+"&");
			bfUrl.append("q=" + URLEncoder.encode(q,"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		url =	bfUrl.toString();
		System.out.println("url = " + url);		
		searchResp = HttpClientUtil.getHttp(url);
		System.out.println("searchResp = " + searchResp);
		
		if(searchResp != null && !"".equals(searchResp)) {
			SAXReader reader = new SAXReader();
		
			try {
				StringReader read = new StringReader(searchResp);
				
				InputSource source = new InputSource(read);
				
				Document document = reader.read(source);
				
				Element root = document.getRootElement();
				int counter  =0;
				int counterOut  =0;
				for ( Iterator i = root.elementIterator(); i.hasNext();) {
					Element elementstat = (Element) i.next();

					if (elementstat.getName().equals("lines")) {
						for (Iterator ieson = elementstat.elementIterator(); ieson.hasNext();) {
							
				        	Element elementSon = (Element) ieson.next();
				        	System.out.println("elementSon.elementText(\"name\") =" +elementSon.elementText("name"));
				        	System.out.println("elementSon.elementText(\"name\").indexOf(q)=" + elementSon.elementText("name").indexOf(q));
				        	if  (	(elementSon.elementText("name").indexOf(q.toLowerCase()) ==0) || 
				        			(elementSon.elementText("name").indexOf(q.toUpperCase()) ==0)) {
								BusLine mybusLine = new BusLine();
								mybusLine.setName(elementSon.elementText("name"));
								mybusLine.setInfo(elementSon.elementText("info"));
								mybusLine.setStats( elementSon.elementText("stats"));
								list.add(mybusLine);	
								counter++;
				        	}
				        	counterOut++;
						}
			        }
			    }
				System.out.println("counter ="+counter);
				System.out.println("counterOut ="+counterOut);	
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return list;
	}	
	/* (non-Javadoc)
	 * @see com.ifp.weixin.biz.core.impl.BusLineSearch#SearchBusLine1(java.lang.String, java.lang.String, int)
	 */
	public String SearchBusLineStr(String city,String q,int with_xys) {

		System.out.println("begin............SearchBusLineStr");
		List<BusLine> list = new ArrayList<BusLine>();
		StringBuffer sBuffer = new StringBuffer();
		list = SearchBusLine( city, q, with_xys);
		if ( list.size()>1 ) {
			sBuffer.append("为您搜索到以下结果：").append("\n");  
			
			for(int i = 0;i < list.size(); i ++){
				BusLine mybusLine = new BusLine();
				mybusLine = list.get(i);
				sBuffer.append(mybusLine.getName()).append(":\n"); 
				sBuffer.append(mybusLine.getStats()).append("\n\n");  
			}
		} else {
			
			sBuffer.append("对不起，查询不到您输入的信息，请重新按照格式输入").append("\n");
		}
		
		
		return sBuffer.toString();
	}	
}
